package com.zzs.elm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.zzs.elm.po.DeliveryAddress;
import com.zzs.elm.service.DeliveryAddressService;

public class DeliveryAddressController {
	
	private DeliveryAddressService deliveryAddressService = new DeliveryAddressService();
	public List<DeliveryAddress> listDeliveryAddressByUserId(HttpServletRequest request) throws Exception {
		String userId = request.getParameter("userId");
		return deliveryAddressService.listDeliveryAddressByUserId(userId);
	}
	
	public DeliveryAddress getEntity (HttpServletRequest request) throws Exception {
		DeliveryAddress deliveryAddress = new DeliveryAddress();
		Integer daId = null;
		if (request.getParameter("daId") != null)
			daId = Integer.valueOf(request.getParameter("daId"));
		String contactName = request.getParameter("contactName");;
		Integer contactSex = Integer.valueOf(request.getParameter("contactSex"));
		String contactTel = request.getParameter("contactTel");
		String address = request.getParameter("address");
		String userId = request.getParameter("userId");
		deliveryAddress.setAddress(address);
		deliveryAddress.setContactName(contactName);
		deliveryAddress.setContactSex(contactSex);
		deliveryAddress.setContactTel(contactTel);
		deliveryAddress.setDaId(daId);
		deliveryAddress.setUserId(userId);
		return deliveryAddress;
	}
	
	public int saveDeliveryAddress(HttpServletRequest request) throws Exception {
		DeliveryAddress deliveryAddress = getEntity(request);
		return deliveryAddressService.saveDeliveryAddress(deliveryAddress);
	}
	
	public DeliveryAddress getDeliveryAddressById(HttpServletRequest request) throws Exception {
		Integer daId = Integer.valueOf(request.getParameter("daId"));
		return deliveryAddressService.getDeliveryAddressById(daId);
	}
	
	public int updateDeliveryAddress(HttpServletRequest request) throws Exception {
		DeliveryAddress deliveryAddress = getEntity(request);
		return deliveryAddressService.updateDeliveryAddress(deliveryAddress);
	}
	
	public int removeDeliveryAddress(HttpServletRequest request) throws Exception {
		Integer daId = Integer.valueOf(request.getParameter("daId"));
		return deliveryAddressService.removeDeliveryAddress(daId);
	}
	
}
